package com.goldenautumn.prefabricate.business.order

import android.app.Application
import androidx.annotation.NonNull
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.goldenautumn.prefabricate.business.entity.AddressInfoVo
import com.goldenautumn.prefabricate.business.entity.CarGoodsInfoVO
import com.goldenautumn.prefabricate.business.entity.CarPreviewInfo
import com.goldenautumn.prefabricate.business.entity.OrderListReqParams
import com.goldenautumn.prefabricate.business.user.LoadingUiState
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import qncx.external.common.Global
import qncx.external.common.base.BaseViewModel

class OrderViewModel (@NonNull application: Application) : BaseViewModel(application) {

    private val mRepository: OrderRepositoryImpl = OrderRepositoryImpl()

    val orderListRequests: Channel<OrderListUiState> = Channel(Channel.UNLIMITED)

    val orderPayUiState: Channel<OrderPayUiState> = Channel(Channel.UNLIMITED)

    val orderStatusUiState: Channel<OrderStatusUiState> = Channel(Channel.UNLIMITED)

    val loading: Channel<LoadingUiState> = Channel(Channel.UNLIMITED)

    fun getOrderList(page:Int,status:String){
        viewModelScope.launch {
            launchFlow(errorCall = {
            }, requestCall = {
                mRepository.getOrderList(OrderListReqParams(status,10,page))
            }, {
                loading.trySend(LoadingUiState.LoadingUI(it))
            }) { order ->
                order.list?.data?.let {
                    orderListRequests.trySend(OrderListUiState.OrderList(it))
                }
            }
        }
    }

    fun getCarPreview(ids:String,type:String,payType:String? = null){
        viewModelScope.launch {
            launchFlow(errorCall = {
                  if(type == "POST") orderPayUiState.trySend(OrderPayUiState.PlayResult(false,"$it"))
            }, requestCall = {
                if(type == "POST") mRepository.onCarPreviewPost(ids,true,payType)
                else mRepository.onCarPreview(ids,false,payType)
            }, {
                loading.trySend(LoadingUiState.LoadingUI(it))
            }) { order ->
                if(type == "POST") orderPayUiState.trySend(OrderPayUiState.PlayResult(true,""))
                else if(order is CarPreviewInfo) orderPayUiState.trySend(OrderPayUiState.CarPreview(order))
            }
        }
    }

    fun onOrderCanCel(orderSn:String,position:Int) {
        viewModelScope.launch {
            launchFlow(errorCall = {
                  orderStatusUiState.trySend(OrderStatusUiState.ActionMsg(position,it))
            }, requestCall = {
                mRepository.onOrderCanCel(orderSn)
            }, {
                loading.trySend(LoadingUiState.LoadingUI(it))
            }) { _ ->
                orderStatusUiState.trySend(OrderStatusUiState.ActionMsg(position))
            }
        }
    }

    fun onOrderDetails(orderSn:String){
        viewModelScope.launch {
            launchFlow(errorCall = {}, requestCall = {
                mRepository.onOrderDetails(orderSn)
            }, {
                loading.trySend(LoadingUiState.LoadingUI(it))
            }) { info ->
                info.order?.let { orderStatusUiState.trySend(OrderStatusUiState.OrderDetails(it)) }
            }
        }
    }

    fun onOrderPay(orderSn:String,payType:String){
        viewModelScope.launch {
            launchFlow(errorCall = {
                orderPayUiState.trySend(OrderPayUiState.PlayResult(false,"$it"))
            }, requestCall = {
                mRepository.onOrderPay(orderSn,payType)
            }, {
                loading.trySend(LoadingUiState.LoadingUI(it))
            }) { info ->
                orderPayUiState.trySend(OrderPayUiState.PlayResult(true,""))
            }
        }
    }

    fun onOrderReceiving(orderSn:String){
        viewModelScope.launch {
            launchFlow(errorCall = {
                orderStatusUiState.trySend(OrderStatusUiState.ActionMsg(0,"$it"))
            }, requestCall = {
                mRepository.onOrderReceiving(orderSn)
            }, {
                loading.trySend(LoadingUiState.LoadingUI(it))
            }) { info ->
                orderStatusUiState.trySend(OrderStatusUiState.ActionMsg(0))
            }
        }
    }

    fun onExpressInfo(orderSn:String){
        viewModelScope.launch {
            launchFlow(errorCall = {}, requestCall = {
                mRepository.onExpressInfo(orderSn)
            }, {
                loading.trySend(LoadingUiState.LoadingUI(it))
            }) { info ->
                info.express?.let { orderStatusUiState.trySend(OrderStatusUiState.ExpressInfo(it)) }
            }
        }
    }

    fun onOrderRefund(type: String,goodsID: String,content: String){
        viewModelScope.launch {
            launchFlow(errorCall = {
                orderStatusUiState.trySend(OrderStatusUiState.ActionMsg(0,it))
            }, requestCall = {
                mRepository.onOrderRefund(type,goodsID,content)
            }, {
                loading.trySend(LoadingUiState.LoadingUI(it))
            }) { info ->
                orderStatusUiState.trySend(OrderStatusUiState.ActionMsg(0,null))
            }
        }
    }

    fun onRefundList(type: String){
        viewModelScope.launch {
            launchFlow(errorCall = {}, requestCall = {
                mRepository.onRefundList(type)
            }, {
                loading.trySend(LoadingUiState.LoadingUI(it))
            }) { info ->
                info.list?.data?.let { orderListRequests.trySend(OrderListUiState.RefundList(it)) }
            }
        }
    }

    fun getRefundDetails(type: String){
        viewModelScope.launch {
            launchFlow(errorCall = {}, requestCall = {
                mRepository.onRefundDetails(type)
            }, {
                loading.trySend(LoadingUiState.LoadingUI(it))
            }) { info ->
                info?.detail?.let { orderStatusUiState.trySend(OrderStatusUiState.RefundDetails(it)) }
            }
        }
    }

}